/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef TIMER_REG_ACCESS_H
#define TIMER_REG_ACCESS_H

/*!
 * @file timer_reg_access.h
 * @brief This file declares or defines timer register access functions
 */

/*******Includes***************************************************************/
#include "device_registers.h"
#include "timer_drv.h"

/*******Definitions************************************************************/
/* Control Register Bit Fields */
#define TIMER_CR_TEN(x)         (((uint32_t)(((uint32_t)(x)) << TIMER_CR_TEN_Pos)) & TIMER_CR_TEN_Msk)
#define TIMER_CR_TIE(x)         (((uint32_t)(((uint32_t)(x)) << TIMER_CR_TIE_Pos)) & TIMER_CR_TIE_Msk)

/* Prescaler Register Bit Fields */
#define TIMER_PSCR_PSCVAL(x)    (((uint32_t)(((uint32_t)(x)) << TIMER_PSCR_PSCVAL_Pos)) & TIMER_PSCR_PSCVAL_Msk)

/* Compare Register Bit Fields */
#define TIMER_CMPR_CMPVAL(x)    (((uint32_t)(((uint32_t)(x)) << TIMER_CMPR_CMPVAL_Pos)) & TIMER_CMPR_CMPVAL_Msk)

/* Counter Register Bit Fields */
#define TIMER_CNTR_CNTVAL(x)    (((uint32_t)(((uint32_t)(x)) << TIMER_CNTR_CNTVAL_Pos)) & TIMER_CNTR_CNTVAL_Msk)

/* Status Register Bit Fields */
#define TIMER_SR_CMPF(x)        (((uint32_t)(((uint32_t)(x)) << TIMER_SR_CMPF_Pos)) & TIMER_SR_CMPF_Msk)

/*******APIs*******************************************************************/
#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @brief Enable Timer counter
 *
 * @param[in] regBase: Timer register base address
 * @param[in] enable: true: Enable timer
 *                    false: Disable timer
 * @return None
 */
static inline void TIMER_REG_SetEnable(TIMER_Type* regBase, bool enable)
{
    uint32_t regValue = regBase->CR;

    regValue &= (~TIMER_CR_TEN_Msk);
    regValue |= TIMER_CR_TEN(enable ? 1U : 0U);
    regBase->CR = regValue;
}

/*!
 * @brief Get Timer state
 *
 * @param[in] regBase: Timer register base address
 * @return true: Timer is enabled or running
 *         false: Timer is disable or stopped
 */
static inline bool TIMER_REG_IsEnabled(const TIMER_Type* regBase)
{
    uint32_t regValue = regBase->CR;

    regValue = (regValue & TIMER_CR_TEN_Msk) >> TIMER_CR_TEN_Pos;

    return (regValue == 1U) ? true : false;
}

/*!
 * @brief Set timer interrupt enable or disable
 *
 * @param[in] regBase: Timer register base address
 * @param[in] enable: true: Enable interrupt
 *                    false: Disable interrupt
 * @return None
 */
static inline void TIMER_REG_SetIntEnable(TIMER_Type* regBase, bool enable)
{
    uint32_t regValue = regBase->CR;

    regValue &= (~TIMER_CR_TIE_Msk);
    regValue |= TIMER_CR_TIE(enable ? 1U : 0U);
    regBase->CR = regValue;
}

/*!
 * @brief Get timer interrupt state
 *
 * @param[in] regBase: Timer register base address
 * @return true: Interrupt is enabled
 *         false: Interrupt is disabled
 */
static inline bool TIMER_REG_IsIntEnabled(TIMER_Type* regBase)
{
    uint32_t regValue = regBase->CR;

    regValue = (regValue & TIMER_CR_TIE_Msk) >> TIMER_CR_TIE_Pos;

    return (regValue == 1U) ? true : false;
}

/*!
 * @brief Set prescaler value
 *
 * @param[in] regBase: Timer register base address
 * @param[in] prescale: Prescaler value
 * @return None
 */
static inline void TIMER_REG_SetPrescaler(TIMER_Type* regBase, uint8_t prescaler)
{
    uint32_t regValue = regBase->PSCR;

    regValue &= (~TIMER_PSCR_PSCVAL_Msk);
    regValue |= TIMER_PSCR_PSCVAL(prescaler);
    regBase->PSCR = regValue;
}

/*!
 * @brief Get prescaler value
 *
 * @param[in] regBase: Timer register base address
 * @return Prescale value
 */
static inline uint8_t TIMER_REG_GetPrescaler(TIMER_Type* regBase)
{
    uint8_t prescaler = 0U;

    prescaler = (uint8_t)(regBase->PSCR & TIMER_PSCR_PSCVAL_Msk);

    return prescaler;
}

/*!
 * @brief Set compare (or timeout) value
 *
 * @param[in] regBase: Timer register base address
 * @param[in] compareValue: Compare (or timeout) value
 * @return None
 */
static inline void TIMER_REG_SetCompareValue(TIMER_Type* regBase, uint32_t compareValue)
{
    regBase->CMPR = compareValue;
}

/*!
 * @brief Get compare (or timeout) value
 *
 * @param[in] regBase: Timer register base address
 * @return Compare (or timeout) value
 */
static inline uint32_t TIMER_REG_GetCompareValue(TIMER_Type* regBase)
{
    return regBase->CMPR;
}

/*!
 * @brief Get compare (or timeout) flag status
 *
 * @param[in] regBase: Timer register base address
 * @return true: Compare is finish (or timeout occured)
 *         false: Comapre is on-going (or timeout doesn't occur yet)
 */
static inline bool TIMER_REG_GetCompareFlag(TIMER_Type* regBase)
{
    uint32_t regValue = regBase->SR;

    regValue = (regValue & TIMER_SR_CMPF_Msk) >> TIMER_SR_CMPF_Pos;

    return (regValue == 1U) ? true : false;
}

/*!
 * @brief Clear compare (or timeout) flag
 *
 * @param[in] regBase: Timer register base address
 * @return None
 */
static inline void TIMER_REG_ClearCompareFlag(TIMER_Type* regBase)
{
    regBase->SR = TIMER_SR_CMPF_Msk;
}

/*!
 * @brief Get current counter value
 *
 * @param[in] regBase: Timer register base address
 * @return Current counter value
 */
static inline uint32_t TIMER_REG_GetCounterValue(TIMER_Type* regBase)
{
    return regBase->CNTR;
}

#if defined(__cplusplus)
}
#endif

#endif /* TIMER_REG_ACCESS_H */

/*******EOF********************************************************************/

